<template>
  <el-autocomplete
    v-model="row.productName"
    :fetch-suggestions="querySearch"
    placeholder="请输入产品名"
    :trigger-on-focus="false"
    clearable
    @input="clear"
    @clear="clear"
    @select="handleSelect"
  ></el-autocomplete>
</template>
<script>
import {dotData} from '@/utils'
import L from 'lodash'
export default {
  data() {
    return {
      selectItem: {value: ''}
    }
  },
  props: {
    row: {
      type: Object,
      required: true
    }
  },
  created() {
    if (this.row.productIsTwo) { // 初始化判断 后端让传1
      this.$set(this.row, 'productIsTwo', this.row.productIsTwo)
    }
  },
  methods: {
    remoteMethod: L.debounce((query, _this, cb) => {
      let parmas = {
        productName: query
      }
      if (_this.row.manufacturersId) {
        parmas.manufacturersId = _this.row.manufacturersId
      }
      _this.$ajax.get('/ms-warehouse-order/service-order-controller/getManufactureProductByName', parmas).then(response => {
        const success = dotData(response, 'success')
        if (success) {
          let data = dotData(response, 'data')
          let tempArr = []
          if (Array.isArray(data)) {
            data.forEach((v, k) => {
              tempArr.push({
                value: v.productName,
                productId: v.productId,
                productNo: v.productNo,
                manufacturersName: v.manufacturersName,
                manufacturersId: v.manufacturersId
              })
            })
          }
          cb(tempArr)
        }
      })
    }, 1000),
    querySearch(query, cb) {
      this.cargoOwnerLoading = true
      this.remoteMethod(query, this, cb)
    },
    handleSelect(item) {
      this.selectItem = item
      this.$set(this.row, 'productId', item.productId)
      this.$set(this.row, 'productIsTwo', 1)
      this.$set(this.row, 'productNo', item.productNo)
      if (item.manufacturersName) {
        this.$set(this.row, 'manufacturersName', item.manufacturersName)
      }
      if (item.manufacturersId) {
        this.$set(this.row, 'manufacturersId', item.manufacturersId)
      }
    },
    clear() {
      if (this.selectItem['value'] === this.row.productName) {
        return
      }
      this.$set(this.row, 'productId', '')
      this.$set(this.row, 'productIsTwo', '')
      this.$set(this.row, 'productNo', '提交后生成')
    }
  }
}
</script>
